div.cpp
Language: C++
Last Modified: 2021-05-21 1:54:44 AM UTC
File Size: 6735 bytes
Last Modified: 2021-05-21 1:54:44 AM UTC
File Size: 6735 bytes
http://www.penguinstew.ca/example/Division/div.cpp
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const int MAXDEC = 65;
const int NOPRINT = -1;
const int DECPRINT = -2;
int getDigits(int number);
int getNextDigit(int& number);
int main(void) {
string tempInput;
bool replay = true;
//Loop until user quits
while (replay) {
//input
int dividend;
int divisor;
//Digit counts
int endDigits;
int sorDigits;
//The list of current values
int curList[MAXDEC + 1];
//The list of calculated multiplied values
int mulList[MAXDEC + 1];
//The list of digits making up the result
int output[MAXDEC + 1];
int endCopy = 0;
int remain = 0;
int current = 0;
int space = 0;
int base = 4;
//Flags
bool input = false;
bool start = false;
replay = false;
cout << "Format: A / B = C" << endl;
cout << "A and B must be less than 100000000" << endl;
cout << "A and B must be greater than 0" << endl;
//Get the dividend from input
while (!input) {
input = true;
cout << "Please enter a number for A:" << endl;
//Get the input and convert it to an int
cin >> tempInput;
dividend = atoi(tempInput.c_str());
//check if dividend is in correct range
if (dividend <= 0 || dividend >= 100000000) {
input = false;
cout << "Invalid A:" << endl;
}
}
input = false;
//Get the divisor from input
while (!input) {
input = true;
cout << "Please enter a number for B:" << endl;
//Get the input and convert it to an int
cin >> tempInput;
divisor = atoi(tempInput.c_str());
//check if divisor is in correct range
if (divisor <= 0 || divisor >= 100000000) {
input = false;
cout << "Invalid B:" << endl;
}
}
input = false;
//Get the count of the digits in both numbers and make a copy of the dividend
endDigits = getDigits(dividend);
sorDigits = getDigits(divisor);
endCopy = dividend;
//Clear the lists
for (int j = 0; j < MAXDEC + 1; j++) {
output[j] = NOPRINT;
curList[j] = NOPRINT;
mulList[j] = NOPRINT;
}
//Loop through all the digits of the number
for (int i = 0; i < endDigits; i++) {
//Get the new value by adding the remainder and the next digit
current = remain * 10 + getNextDigit(endCopy);
for (int j = 0; j < 10; j++) {
//Find the value for j such that j + 1 multiplied by the divisor is greater than the current value
if ((j + 1) * divisor > current) {
//if j = 0 and we haven't started printing then tell it to print nothing
if (j == 0 && start == false) {
output[i] = NOPRINT;
curList[i] = NOPRINT;
mulList[i] = NOPRINT;
} else {
if (start == false) { //If j != 0 and not started printing, start
start = true;
curList[i] = NOPRINT;
} else { //If this isn't the first line, add current to the list
curList[i] = current;
}
//Determine the results and add them to their perspective lists
mulList[i] = j * divisor;
output[i] = j;
}
//Determine next remainder
remain = current - j * divisor;
break;
}
}
}
if (start == false) { //If still haven't started, set previous output to 0
start = true;
mulList[endDigits - 1] = 0;
output[endDigits - 1] = 0;
}
//Mark the decimal point
output[endDigits] = DECPRINT;
curList[endDigits] = DECPRINT;
mulList[endDigits] = DECPRINT;
//Loop through the decimal digits until maxDec or result is 0
for (int i = endDigits + 1; i <= MAXDEC; i++) {
//Get the new value from the remainder
current = remain * 10;
for (int j = 0; j < 10; j++) {
//Find the value for j such that j + 1 multiplied by the divisor is greater than the current value
if ((j + 1) * divisor > current) {
//Add current to the list
curList[i] = current;
//Determine the results and add them to their perspective lists
mulList[i] = j * divisor;
output[i] = j;
remain = current - j * divisor;
break;
}
}
if (remain == 0) { //If the remainder is 0, set the rest of the output to not print and stop
break;
}
}
cout << endl;
//Spacer for the divisor
for (int i = 0; i < sorDigits + 3; i++) {
cout << " ";
}
//Print the resulting value
for (int i = 0; i < MAXDEC; i++) {
if (output[i] == NOPRINT)
cout << " ";
else if (output[i] == DECPRINT)
cout << ".";
else
cout << output[i];
}
cout << endl;
//Print out the operation
cout << divisor << " / " << dividend << endl;
//Print out the process
for (int i = 0; i < MAXDEC; i++) {
if (mulList[i] == NOPRINT) { //If current number is no print, skip
continue;
}
if (mulList[i] == DECPRINT) { //If current number is decimal, move the base back one
base = base - 1;
continue;
}
//print the current value
if (curList[i] != NOPRINT) {
space = sorDigits + i - getDigits(curList[i]) + base;
for (int j = 0; j < space; j++) {
cout << " ";
}
cout << curList[i] << endl;
}
//Print the multiplied value
space = sorDigits + i - getDigits(mulList[i]) + base;
for (int j = 0; j < space; j++) {
cout << " ";
}
cout << mulList[i] << endl;
}
cout << endl;
cout << "Do you want to divide another number? (Y/N): ";
//Get the input
cin >> tempInput;
//check if string starts with y or Y
if (tempInput[0] == 'y' || tempInput[0] == 'Y') {
replay = true;
}
}
return 0;
}
// Get the number of digits in the number
int getDigits(int number) {
for (int i = 1; i <= 8; i++) {
//Multiply the number by 0.1 and then truncate by casting to an int
number = (int)(number * 0.1);
//Repeat util the number has ran out of digits
if (number == 0) {
//Return i which will be the number of digits
return i;
}
}
return 0;
}
//returns the left most digit
int getNextDigit(int& number) {
//Get the number of digits in the number
int dig = getDigits(number);
//Power and inverse power
int power = 1;
double invPower = 1;
//Loop through the number of digits to get the values
for (int i = 0; i < dig - 1; i++) {
power = power * 10;
invPower = invPower * 0.1;
}
//Get the last digit
int last = (int)(number * invPower);
//Remove the last digit from the number
number = number - (power * last);
return last;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263